package TeacherEvaluation.com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import TeacherEvaluation.com.entity.Department;
import TeacherEvaluation.com.entity.ResultMsg;
import TeacherEvaluation.com.service.departmentService;
/**
 * 院系控制类
 * @author 陈龙
 *
 */
@Controller
public class departmentController {
	//导入departmentService bean
	@Autowired
	departmentService depService;
	
	//添加院系主页
	@RequestMapping("/depAddIndex")
	public ModelAndView depAddIndex() {
		ModelAndView view = new ModelAndView("admin/department/addDepartment");
		return view;
	}

	//添加院系
	@RequestMapping("/depAdd")
	@ResponseBody
	public ResultMsg departmentAdd(String department) {
		System.out.println(department);
		int i = depService.insertDep(department);
		if (i==0) {
			return new ResultMsg(0, "院系已存在");
		}else {
			return new ResultMsg(1, "添加成功");
		}
	}
	
	//查看院系
	@RequestMapping("/depSelect")
	@ResponseBody
	public ModelAndView departmentSelect() {
		//创建视图
		ModelAndView mView = new ModelAndView("admin/department/showDepartment");
		//将查询结果发送给视图
		mView.addObject("itmes", depService.selectDep());
		return mView;
	}
	
	//修改院系主页
	@RequestMapping("/depUpdateIndex")
	public ModelAndView depUpdateIndex(Department department) {
		ModelAndView view = new ModelAndView("admin/department/updateDepartment");
		view.addObject("itmes", department);
		return view;
	}
	
	
	
	//修改院系
	@RequestMapping("/depUpdate")
	@ResponseBody
	public ResultMsg departmentUpdate(Department department) {
		int i = depService.updateDep(department);
		//查询修改的院系名是否存在
		if (i==0) {
			return new ResultMsg(0, "删除失败");
		}else {
			return new ResultMsg(1, "删除成功");
		}
	}
	
	//删除院系
	@RequestMapping("/depDelete")
	@ResponseBody
	public ResultMsg depDelete(String dep_id) {
		int i = depService.deleDep(Integer.parseInt(dep_id));
		if (i==0) {
			return new ResultMsg(0,"删除失败");
		}else {
			return new ResultMsg(1,"删除成功");
		}
	}

}
